package oop.controller.action.user;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oop.controller.action.DefaultAction;
import oop.db.Database;

public abstract class UserManagerAction extends DefaultAction {

	protected ResultSet getUserOnSQL(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Connection conn;
		try {
			conn = Database.get().getConnection();
			Statement stmt = conn.createStatement();
			String sql = "SELECT user_name, user_pass " + "FROM tblUser "
					+ "WHERE user_name = '" + request.getParameter("userName")
					+ "'";
			ResultSet rs = stmt.executeQuery(sql);
			return rs;

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	protected ResultSet getUserEmailOnSQL(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Connection conn;
		try {
			conn = Database.get().getConnection();
			Statement stmt = conn.createStatement();
			String sql = "SELECT user_name, user_email " + "FROM tblUser "
					+ "WHERE user_email = '"
					+ request.getParameter("userEmail") + "'";
			ResultSet rs = stmt.executeQuery(sql);
			return rs;

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	protected ResultSet getUserPassOnSQL(String userName) throws ServletException, IOException {
		Connection conn;
		try {
			conn = Database.get().getConnection();
			Statement stmt = conn.createStatement();
			String sql = "SELECT user_name, user_pass " + "FROM tblUser "
					+ "WHERE user_name = '" + userName
					+ "'";
			ResultSet rs = stmt.executeQuery(sql);
			return rs;

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	protected boolean isExist(ResultSet rs) throws ServletException,
			IOException {

		try {
			if (rs.next())
				return true;
			else
				return false;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	protected boolean isUserExist(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ResultSet rs = getUserOnSQL(request, response);
		return isExist(rs);
	}

	protected boolean isEmailExist(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		ResultSet rs = getUserEmailOnSQL(request, response);
		return isExist(rs);
	}

	protected boolean isEmpty(String inStr) {
		return  (inStr == null)
				||(inStr.compareTo("")) == 0 || inStr.length() == 0;
	}

	protected boolean isValidEmail(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter("userEmail");
		String emailExp = "[a-zA-Z0-9-._]+@+[a-zA-Z0-9.-_]+.+[a-zA-z0-9]{2,4}";
		if (email.matches(emailExp) && (isEmpty(email) == false)) {
			return true;
		} else {
			return false;
		}
	}

}
